﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AwesomeDataLager;

namespace AwesomeSSPLSLogik
{
   public class GameHandler
   {
       /// <summary>
       /// Creates a not randomized gamesession. A gamesession(Gamesession) is created and a 
       /// playergamesession(SpelaresSpelsessioner) is added.
       /// </summary>
       /// <param name="playerId"></param>
       /// <param name="gameId"></param>
       /// <param name="rounds">number of rounds the player wants to play</param>
       /// <returns>boolean</returns>
       public static bool CreateNonrandomGamesession(int playerId, int gameId, int rounds)
       {
           var player = GetDBInfo.GetPlayerById(playerId);
           var game = GetDBInfo.GetGameById(gameId);

           int gameSessionId = SaveDBInfo.CreateGameSession(game.ID, rounds);

           int gamePlayerSessionId = SaveDBInfo.CreatePlayerSession(gameSessionId, player.ID);
           if ((gameSessionId != null && gamePlayerSessionId != null) || (gamePlayerSessionId > 0 && gameSessionId > 0))
               return true;
           else
               return false;
       }

       public static bool DeletePlayerSession(int playerId, int sessionId)
       {
           if (SaveDBInfo.DeletePlayerSession(playerId, sessionId))
               return true;
           else
               return false;
       }

       public static bool DeleteGameSession(int gameSessionId)
       {
           if (SaveDBInfo.DeleteGameSession(gameSessionId))
               return true;
           else
               return false;
       }

       public static List<SpelSession> GetSpelSessionListaLogik(string namn)
       {
           var spelSession = GetDBInfo.GetGameSessionListInfoDB(namn);



           var spelLista = new List<GameSession>();
           //foreach (var s in (IEnumerable) spel)
           //{
             //  spelLista.Add(new GameSession{Rounds = s.antalomgångar, GameObject = {new GameObject{GameId = s.spelid, GameName = "vet i fan"}}, GameSessionId = s.id;
               
           //}

            //spelLista.Add(new GameSession{Rounds = 3, GameSessionId = 22, GameObject = new GameObject{GameId = 1, GameName = spel.ToString()}});

           return spelLista;
       }

       public static List<Game> GetGameList()
       {
           var games = GetDBInfo.GetGameListInfoDb();

           return  games.Select(game => new Game {GameId = game.Id, GameName = game.Name}).ToList();
       }


       //public static List<Gamesession> GetAllActiveGameSessionsLogic()
       //{
       //    var sessions = GetDBInfo.GetActiveGameSessionInfoDb();
       //     //return sessions.Select 
       //}

       public static bool UpdateGameRounds(int id, int rounds)
       {
           //return true;
           try
           {
               var game = new GameDbEntities();
               var gameSession = game.GameSession.Where(s => s.ID == id).SingleOrDefault();

               gameSession.NoOfGameRounds = rounds;

               game.SaveChanges();

               return true;
           }
           catch
           {
               return false;
           }
       }
   }
}
 